// pages/orderdetails/orderdetails.js
let network = require("../../utils/network.js")
let app = getApp();
let url  = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderDetail:{},
    out_trade_no:'',
    discount: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.orderDetail(options.out_trade_no)
    that.setData({
      discount: wx.getStorageSync('discount')
    })
  },
  orderDetail(dh){
    var that = this
    // 订单详情
    network.request({
      url: url +'/AppletsApi/Order/orderDetail.html',
      data: {
        applet_openid: wx.getStorageSync('applet_openid'),
        out_trade_no: dh,
      },
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        that.setData({
          orderDetail: res.data.data
        })
      }
    })
  },


  qxdd: function (){
    // 取消订单
    var that = this
    network.request({
      url: url +'/AppletsApi/Order/orderCancel.html',
      data: {
        applet_openid: wx.getStorageSync('applet_openid'),
        out_trade_no: that.data.orderDetail.out_trade_no,
      },
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        that.orderDetail(that.data.orderDetail.out_trade_no)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  fk:function(){
    var that = this
    network.request({
      url: url +'/AppletsApi/Order/orderWechatPay.html',
      data: {
        applet_openid: wx.getStorageSync('applet_openid'),
        out_trade_no: that.data.orderDetail.out_trade_no,
        paytype: 1,
      },
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (data) {
        // 调用支付接口
        wx.requestPayment({
          timeStamp: data.data.timeStamp,
          nonceStr: data.data.nonceStr,
          package: data.data.package,
          signType: data.data.signType,
          paySign: data.data.paySign,
          success(res) {
            wx.navigateTo({
              url: '/pages/myorder/myorder?state=2',
            })
          },
          fail(res) {
            wx.navigateTo({
              url: '/pages/myorder/myorder?state=1',
            })
          }
        })

      }
    })
  }
})